//
//  FirstTopViewViewController.m
//  Project_B
//
//  Created by 张闯的mac pro on 15/11/25.
//  Copyright © 2015年 张闯. All rights reserved.
//

#import "FirstTopViewViewController.h"
#import "AFNetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "FirstModel.h"
#import "CalculateHeight.h"
#import "CommentTableViewCell.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
#import "ThirdlubJumpViewController.h"
#import "UIImage+GIF.h"

@interface FirstTopViewViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *topTableView;
@property (nonatomic, strong) NSMutableDictionary *topDic;

@property (nonatomic, strong) NSMutableArray *commentArr;
@property (nonatomic, assign) NSInteger i;
@property (nonatomic, strong) MBProgressHUD *hud;

@end

@implementation FirstTopViewViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"话题精选";
    
   
    self.topDic = [NSMutableDictionary dictionary];
    self.commentArr = [NSMutableArray array];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-fanhui@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    [self getTopViewData];
    [self getCommentData];
    [self createTopTabelView];
    
    self.i = 1;
    
    __weak typeof(self) weakSelf = self;
//
    self.topTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        __strong typeof(self)  strongSelf = weakSelf;
        if (strongSelf) {
            [strongSelf footRefresh];
        } else{
            NSLog(@"weak");
        }
//
    }];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:NO];
    self.hud.animationType = MBProgressHUDAnimationFade;
    self.hud.mode = MBProgressHUDModeIndeterminate;
<<<<<<< HEAD
    self.hud.animationType = MBProgressHUDAnimationFade;
    self.hud.mode = MBProgressHUDModeIndeterminate;
=======
    
>>>>>>> a1c9e5d1f262dc248cd920fdd98c55df3f101174
}

- (void)createTopTabelView
{
    UIView *headView = [[UIView alloc] init];
    
    if ([self.typeStr isEqualToString:@"1"]) {
        
        headView.frame = CGRectMake(0, 0, 375, 280);
        [self.view addSubview:headView];
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 180)];
        imgView.tag = 1001;
        [headView addSubview:imgView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 185, 300, 30)];
        titleLabel.tag = 1002;
        titleLabel.textColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
        [headView addSubview:titleLabel];
        
        UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 225, 300, 20)];
        timeLabel.tag = 1003;
        timeLabel.textColor = [UIColor grayColor];
        [timeLabel setFont:[UIFont systemFontOfSize:13]];
        [headView addSubview:timeLabel];
        
        UILabel *placeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 250, 300, 20)];
        placeLabel.tag = 1004;
        placeLabel.textColor = [UIColor grayColor];
        [placeLabel setFont:[UIFont systemFontOfSize:13]];
        [headView addSubview:placeLabel];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 270, 375, 0.5)];
        label.backgroundColor = [UIColor grayColor];
        [headView addSubview:label];
        
    } else  {
        
        headView.frame = CGRectMake(0, 0, 375, 90);
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 375 - 20, 60)];
        titleLabel.numberOfLines = 0;
        titleLabel.tag = 2001;
        [titleLabel setFont:[UIFont systemFontOfSize:19]];
        titleLabel.textColor = [UIColor colorWithRed:60.0 / 255 green:149.0 / 255 blue:58.0 / 255 alpha:1];
        [headView addSubview:titleLabel];
        
        UILabel *fromLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 200, 25)];
        fromLabel.tag = 2002;
        fromLabel.userInteractionEnabled = YES;
        [fromLabel setFont:[UIFont systemFontOfSize:14]];
        fromLabel.textColor = [UIColor orangeColor];
        [headView addSubview:fromLabel];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        [fromLabel addGestureRecognizer:tap];
        
        
        UILabel *replayLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 60, 80, 25)];
        replayLabel.tag = 2003;
        [replayLabel setFont:[UIFont systemFontOfSize:14]];
        replayLabel.textColor = [UIColor grayColor];
        [headView addSubview:replayLabel];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 85, 375, 0.5)];
        label.backgroundColor = [UIColor grayColor];
        [headView addSubview:label];
    }
    
    //tableView
    self.topTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    self.topTableView.delegate = self;
    self.topTableView.dataSource = self;
    self.topTableView.tableHeaderView = headView;
    [self.view addSubview:self.topTableView];
    [self.topTableView registerClass:[CommentTableViewCell class] forCellReuseIdentifier:@"cell"];
}

//获取headView数据
- (void)getTopViewData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v3/club/topics/%@/", self.idString];
    
    NSString *bodyStr = [NSString stringWithFormat:@"topic_id=%@", self.idString];
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    
    [AFNetworkHandler requestPOSTWithURL:str body:dataBody comletion:^(id result) {
        
        self.topDic = result[@"data"];
        [self getHeaderViewData:self.topDic];
        [self.topTableView reloadData];
        [self.hud removeFromSuperview];
    }];
   
}

- (void)getHeaderViewData:(NSDictionary *)dic
{
    if ([self.typeStr isEqualToString:@"1"]) {
        
        UIImageView *imgView = (UIImageView *)[self.view viewWithTag:1001];
        [imgView sd_setImageWithURL:[NSURL URLWithString:((NSArray *)dic[@"topic_image"]).firstObject[@"image"]] placeholderImage:[UIImage imageNamed:@""]];
        
        UILabel *titleLabel = (UILabel *)[self.view viewWithTag:1002];
        titleLabel.text = dic[@"title"];
        
        UILabel *timeLabel = (UILabel *)[self.view viewWithTag:1003];
        timeLabel.text = [NSString stringWithFormat:@"活动时间: %@", dic[@"times"]];
        
        UILabel *placrLabel = (UILabel *)[self.view viewWithTag:1004];
        placrLabel.text = [NSString stringWithFormat:@"活动地点: %@", dic[@"aimaddress"]];
        
    } else {
        
        UILabel *titleLabel = (UILabel *)[self.view viewWithTag:2001];
        titleLabel.text = dic[@"title"];
        
        UILabel *fromLabel = (UILabel *)[self.view viewWithTag:2002];
        fromLabel.text = [NSString stringWithFormat:@"%@>", dic[@"club_title"]];
        
        UILabel *replayLabel = (UILabel *)[self.view viewWithTag:2003];
        replayLabel.text = [NSString stringWithFormat:@"%@个回复", dic[@"replytopic"]];
    }
}

//加载
- (void)footRefresh
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/replylist/%@/?page=%ld?page=0&topic_id=%@", self.idString, self.i++, self.idString];
    
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
        
        NSArray *arr = [FirstModel transformWithArray:result[@"data"][@"topiclist"]];
        [self.commentArr addObjectsFromArray:arr];
        [self.topTableView reloadData];
        [self.topTableView.mj_footer endRefreshing];
    }];
}


//获取评论区数据
- (void)getCommentData
{
    NSString *str = [NSString stringWithFormat:@"http://api.lis99.com/v2/club/replylist/%@/?page=0?page=0&topic_id=%@", self.idString, self.idString];
    
    [AFNetworkHandler getUrl:str cookie:nil result:^(id result) {
       
        self.commentArr = [FirstModel transformWithArray:result[@"data"][@"topiclist"]];
        [self.topTableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.commentArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat h = [CalculateHeight heightWithText:((FirstModel *)self.commentArr[indexPath.row]).content fontSize:14];
    
    FirstModel *temp = [[FirstModel alloc] init];
    temp = self.commentArr[indexPath.row];
    CGFloat h1 = [((NSArray *)temp.topic_image).firstObject[@"height"] floatValue];
   // CGFloat w1 = [((NSArray *)temp.topic_image).firstObject[@"width"] floatValue];
    
    CGFloat sum;
    if (h1 != 0) {
        sum = h + 100 + 280;
    } else {
        sum = h + 80;
    }
    return sum;
}

#pragma mark - tapAction
- (void)tapAction
{
    ThirdlubJumpViewController *lubVC = [[ThirdlubJumpViewController alloc] init];
    lubVC.idString = [self.commentArr[0] club_id];
    lubVC.model = self.commentArr[0];
    [self.navigationController pushViewController:lubVC animated:YES];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
